package com.hamilton.erik.pt.core.model;

import java.io.Serializable;
import java.util.Comparator;

import com.hamilton.erik.pt.core.enumeration.Gender;
import com.hamilton.erik.pt.core.util.StringUtil;

public class Animal implements Serializable {

	private static final long serialVersionUID = 970593285779679895L;
	
	private Long id;
	private String userName;
	private String type;
	private String genus;
	private String species;
	private String name;
	private String birthDate;
	private String purchaseDate;
	private Gender gender;
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	public String getUserName() {
		return userName;
	}
	
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	public String getGenus() {
		return genus;
	}
	
	public void setGenus(String genus) {
		this.genus = genus;
	}
	
	public String getSpecies() {
		return species;
	}
	
	public void setSpecies(String species) {
		this.species = species;
	}
	
	public String getScientificName() {
		StringBuilder scientificName = new StringBuilder("");
		
		if(!StringUtil.isNullOrEmpty(genus)) {
			scientificName.append(genus).append(" ");

			if(!StringUtil.isNullOrEmpty(species)) {
				scientificName.append(species);
			}
			else {
				// unknown species
				scientificName.append("sp.");
			}
		}

		return scientificName.toString();
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getBirthDate() {
		return birthDate;
	}
	
	public void setBirthDate(String birthDate) {
		this.birthDate = birthDate;
	}
	
	public String getPurchaseDate() {
		return purchaseDate;
	}
	
	public void setPurchaseDate(String purchaseDate) {
		this.purchaseDate = purchaseDate;
	}
	
	public Gender getGender() {
		return gender;
	}
	
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	
	public void setGender(String gender) {
		this.gender = Gender.fromValue(gender);
	}
	
	public static Comparator<Animal> NameTypeGenusSpeciesComparator = new Comparator<Animal>() {
		public int compare(Animal first, Animal second) {
			String firstName = first.name.toLowerCase();
			String secondName = second.name.toLowerCase();
			
			return firstName.compareTo(secondName);
		}
	};
	
	public static Comparator<Animal> TypeNameGenusSpeciesComparator = new Comparator<Animal>() {
		public int compare(Animal first, Animal second) {
			String firstType = first.type.toLowerCase();
			String secondType = second.type.toLowerCase();
			
			int c = firstType.compareTo(secondType);
			
			// Always sort by Name lastly
			if(c == 0) {
				String firstName = first.name.toLowerCase();
				String secondName = second.name.toLowerCase();
				
				c = firstName.compareTo(secondName);
			}
			
			return c;
		}
	};
	
	public static Comparator<Animal> GenusSpeciesNameTypeComparator = new Comparator<Animal>() {
		public int compare(Animal first, Animal second) {
			String firstScientificName = first.getScientificName().toLowerCase();
			String secondScientificName = second.getScientificName().toLowerCase();
			
			int c = firstScientificName.compareTo(secondScientificName);
						
			// Always sort by Name lastly
			if(c == 0) {
				String firstName = first.name.toLowerCase();
				String secondName = second.name.toLowerCase();
				
				c = firstName.compareTo(secondName);
			}
			
			return c;
		}
	};
}
